#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <fstream>

using namespace std;

/**
 * @brief simplest_yuv420_split             取Y, U, V 分量进行各自保存
 * @param url
 * @param w
 * @param h
 * @param num
 * @return
 */
int simplest_yuv420_split(const char *url, int w, int h, int num)
{
    // 读文件
    ifstream ifile;         //yuv420 file
    ifile.open(url, ios::in | ios::binary);
    ofstream ofile1, ofile2, ofile3; //store y,u,v bits
    ofile1.open("./output_1080_y.y", ios::out | ios::binary);
    ofile2.open("./output_1080_u.y", ios::out | ios::binary);
    ofile3.open("./output_1080_v.y", ios::out | ios::binary);

    char* pic = (char*)malloc(w*h * 3/2);
    memset(pic, 0, w*h * 3/2);
    ifile.seekg(ios::beg);
    for (int i = 0; i < num; i++)
    {
        ifile.read(pic, w*h * 3/2);
        ofile1.write(pic, w*h);
        ofile2.write(pic + w*h, w*h / 4);
        ofile3.write(pic + w * h * 5 / 4, w*h/4);
    }

    free(pic);
    ifile.close();
    ofile1.close();
    ofile2.close();
    ofile3.close();

    cout <<"Finish.\n";
    return 0;
}


/**
 * @brief main          测试YUV分量
 * @return
 */
int main(void)
{
    simplest_yuv420_split("1920x1080_nv12.yuv", 1920, 1080, 1);
    return 0;
}

